package cn.asiontang.xposed.url_firewall;

import java.net.URL;

import cn.asiontang.xposed.LogEx;
import cn.asiontang.xposed.XposedUtils;
import de.robv.android.xposed.XC_MethodHook;
import de.robv.android.xposed.callbacks.XC_LoadPackage;

/**
 *
 */
public class XposedHookClass_HttpURLConnectionImpl
{
    private static final String TAG = XposedHookClass_HttpURLConnectionImpl.class.getName();

    /**
     * <pre>
     * 此方法优点:
     *      1.能捕获 HTTPS 协议
     * 此方法缺点:
     *      1.不能捕获 ApacheHttp 发起的请求
     * </pre>
     */
    public static void hook(final XC_LoadPackage.LoadPackageParam loadPackageParam)
    {
        try
        {
            XposedUtils.realHookAllMethods("java.net.URL", loadPackageParam.classLoader, "openConnection", new XC_MethodHook()
            {
                @Override
                protected void beforeHookedMethod(final MethodHookParam param) throws Throwable
                {
                    URL url = (URL) param.thisObject;
                    LogEx.log(TAG, "成功捕获网址:", "URL.openConnection", url);

                    //不是白名单里的网址就不允许请求.
                    //param.setThrowable(new SocketTimeoutException());
                }
            });
            //XC_MethodHook methodHook = new XC_MethodHook()
            //{
            //    @Override
            //    protected void beforeHookedMethod(final MethodHookParam param) throws Throwable
            //    {
            //        LogEx.log(TAG, param.method);
            //        //不是白名单里的网址就不允许请求.
            //        //param.setThrowable(new SocketTimeoutException());
            //    }
            //};
            ////源码:
            ////https://android.googlesource.com/platform/libcore/+/757afaa7afe96791a3cc612c9e3c4597a7321c7e/luni/src/main/java/libcore/net/http/HttpURLConnectionImpl.java
            ////https://android.googlesource.com/platform/libcore/+/757afaa7afe96791a3cc612c9e3c4597a7321c7e/luni/src/main/java/libcore/net/http/HttpEngine.java
            ////https://android.googlesource.com/platform/libcore/+/757afaa7afe96791a3cc612c9e3c4597a7321c7e/luni/src/main/java/libcore/net/http/HttpConnection.java
            //Class<?> aClass = XposedHelpers.findClass("libcore.net.http.HttpURLConnectionImpl", loadPackageParam.classLoader);
            //XposedUtils.realHookAllMethods(aClass, "connect", methodHook);
            //XposedUtils.realHookAllMethods(aClass, "getResponseMessage", methodHook);
            //XposedUtils.realHookAllMethods(aClass, "getResponseCode", methodHook);
            //XposedUtils.realHookAllMethods(aClass, "getErrorStream", methodHook);
            //XposedUtils.realHookAllMethods(aClass, "getInputStream", methodHook);
            //XposedUtils.realHookAllMethods(aClass, "getHeaderField", methodHook);
            //XposedUtils.realHookAllMethods(aClass, "getHeaderFieldKey", methodHook);
            //XposedUtils.realHookAllMethods(aClass, "getHeaderFields", methodHook);
        }
        catch (Exception e)
        {
            LogEx.log(TAG, "hook Exception", e);
        }
    }
}
